﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;

namespace RentAWorker.Management.Infrastructure
{
    public static class TypeTrackingExtension
    {
        public static bool CanResolve<T>(this IUnityContainer container)
        {
            return container.Configure<TypeTrackingUnityExtension>().RegisteredTypes.Contains(typeof(T));
        }

        public class TypeTrackingUnityExtension : UnityContainerExtension
        {
            internal List<Type> RegisteredTypes = new List<Type>();

            protected override void Initialize()
            {
                Context.RegisteringInstance += (sender, e) => RegisteredTypes.Add(e.RegisteredType);
                Context.Registering += (sender, e) => RegisteredTypes.Add(e.TypeFrom);
            }

            public override void Remove()
            {
                RegisteredTypes.Clear();
            }
        }
    }
}
